package summit.tests
{
import flash.events.Event;

import summit.basecamp.constants.MilestoneFilter;
import summit.basecamp.helpers.MilestoneHelper;
import summit.data.AccountToken;
import summit.events.BasecampAPIEvent;
import summit.tests.tools.SummitTestContainer;

public class MilestoneHelperTests extends SummitTestContainer
{
	private var _milestoneId:String;
	private var _milestoneXML:XML;
	
	public function MilestoneHelperTests(token:AccountToken)
	{
		super(token);
	}
	
	override protected function self_CompleteInit(e:Event):void
	{
		super.initializeLoader(loader_OKInitPrepareMilestone);
		loader.load(MilestoneHelper.prepare(projectId));
	}
	
	private function loader_OKInitPrepareMilestone(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateMilestone);
		loader.load(MilestoneHelper.create(e.data));
	}
	
	private function loader_OKInitCreateMilestone(e:BasecampAPIEvent):void
	{
		_milestoneId  = e.data.milestone[0].id[0];
		_milestoneXML = e.data.milestone[0];
		super.self_CompleteInit(null);
	}
	
	public function listTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(MilestoneHelper.list(projectId));
	}
/* Known Failures
	public function listCompletedTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(MilestoneHelper.list(projectId, 
		MilestoneFilter.COMPLETED));
	}

	public function listUpcomingTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(MilestoneHelper.list(projectId, 
		MilestoneFilter.UPCOMING));
	}

	public function listLateTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(MilestoneHelper.list(projectId, 
		MilestoneFilter.LATE));
	}
*/	
	public function readTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(MilestoneHelper.read(_milestoneId, projectId));
	}
	
	public function listAllTest(context:Object):void
	{
		super.initializeLoader(super.loader_OKComplete);
		loader.load(MilestoneHelper.list(projectId, 
		MilestoneFilter.ALL));
	}
	
	public function updateTest(content:Object):void
	{
		initializeLoader(super.loader_OKComplete);
		
		var xml:XML = new XML("<request/>");
		xml.appendChild(_milestoneXML);
		
		loader.load(MilestoneHelper.update(xml));
	}
	
	public function completeUncompleteTest(content:Object):void
	{
		initializeLoader(loader_OKCheck);
		loader.load(MilestoneHelper.complete(_milestoneId));
	}
	
	private function loader_OKCheck(e:BasecampAPIEvent):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(MilestoneHelper.uncomplete(_milestoneId));
	}
	
	override public function cleanUp():void
	{
		super.initializeLoader(loader_OKCleanUp);
		loader.load(MilestoneHelper.destroy(_milestoneId));
	}
	
	private function loader_OKCleanUp(e:BasecampAPIEvent):void
	{
		super.cleanUp();
	}
}
}